package com.smile.ye.response;

import lombok.Data;
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.util.HashMap;
import java.util.List;

@Data
public  class Result<T> {

    private Integer status;

    private String message;

    private  T data;
    private Result(){
        this.status = 200;
        this.message = "OK";
        this.data = null;
    }
    private Result(ResultCode resultCode,T data){
        this.status = resultCode.getStatus();
        this.message = resultCode.getMessage();
        this.data = data;
    }
    private Result(String message){
       this.message = message;
       this.status = 500;
    }
    private Result(Integer status, String message){
        this.status = status;
        this.message = message;
    }

    /**
     * 暴露外面静态方法. 成功
     */
    public static <E> Result build(E data){
        return new Result<E>(ResultCode.SUCCESS,data);
    }

    public static <T> Result<T> build(IPage<T> page) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("items",page.getRecords());
        return new Result(ResultCode.SUCCESS,map);
    }

    public static <T> Result<T> build() {
        return new Result();
    }

    /**
     * 失败
     */
    public static Result fail(String message){
        return new Result(message);
    }
    public static Result fail(Integer status, String message){
        return new Result(status, message);
    }
}